import { t } from 'i18next'
import requestMeeting from '@/plugins/axios'
import { DcsInfo } from './interface'
import { transDcsInfo } from './translator'
import { Mt } from '../mt'
import Alert from '@/components/Alert'
import { makeMtParam } from '../type'

export const fetchDcsInfo = async (meetingMoid: string) =>
	requestMeeting<DcsInfo>({
		url: `/mcc/${meetingMoid}/dcs`,
		method: 'get',
	}).then((res) => (res.success && res.data ? transDcsInfo(res.data) : undefined))

/** 开启数据协作 */
export const fetchOpenDcs = async (meetingMoid: string) =>
	requestMeeting<DcsInfo>({
		url: `/mcc/${meetingMoid}/dcs`,
		method: 'post',
	}).then((res) => res)

/** 关闭数据协作 */
export const fetchCancelDcs = async (meetingMoid: string) =>
	requestMeeting<DcsInfo>({
		url: `/mcc/${meetingMoid}/dcs`,
		method: 'delete',
	}).then((res) => res)

/** 添加协作方终端 */
export const fetchAddDcsMts = async (meetingMoid: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/dcs/mts`,
		method: 'post',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('参与协作失败'))
			}
		})
		.catch(() => {
			Alert(t('参与协作失败'))
		})
/** 删除协作方终端*/
export const fetchDeleteDcsMts = async (meeting_id: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/dcs/mts`,
		method: 'DELETE',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('退出协作失败'))
			}
		})
		.catch(() => {
			Alert(t('退出协作失败'))
		})
/** 呼叫协作终端 */
export const fetchCallDcsMts = async (meetingMoid: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/dcs/mts/online`,
		method: 'post',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('呼叫终端失败'))
			}
		})
		.catch(() => {
			Alert(t('呼叫终端失败'))
		})
/** 挂断协作方终端*/
export const fetchHangupDcsMts = async (meeting_id: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/dcs/mts/online`,
		method: 'DELETE',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('挂断终端失败'))
			}
		})
		.catch(() => {
			Alert(t('挂断终端失败'))
		})
/** 添加协作方 */
export const fetchApplyDcsMts = async (meetingMoid: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/dcs/apply/mts`,
		method: 'post',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('添加协作方失败'))
			}
		})
		.catch(() => {
			Alert(t('添加协作方失败'))
		})
/** 取消协作方*/
export const fetchCancelDcsMts = async (meeting_id: string, mts: Mt[]) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/dcs/apply/mts`,
		method: 'DELETE',
		data: { mts: mts.map((x) => makeMtParam(x)) },
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('取消协作方失败'))
			}
		})
		.catch(() => {
			Alert(t('取消协作方失败'))
		})
/** 同意协作 */
export const fetchApplyDcs = async (meetingMoid: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meetingMoid}/dcs/apply`,
		method: 'post',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('同意协作失败'))
			}
		})
		.catch(() => {
			Alert(t('同意协作失败'))
		})
/** 拒绝协作*/
export const fetchRefuseDcs = async (meeting_id: string, mt: Mt) =>
	requestMeeting({
		url: `/mcc/${meeting_id}/dcs/apply`,
		method: 'DELETE',
		data: makeMtParam(mt),
	})
		.then((res) => {
			if (!res.success) {
				Alert(t('拒绝协作失败'))
			}
		})
		.catch(() => {
			Alert(t('拒绝协作失败'))
		})
